package com;

import java.util.HashSet;

public class Exercise2 {
    public static void main(String[] args) {
        int[] arr = {100, 3, 7, 6, 5, 4, 200, 1, 3, 2};
        int maxLength = MLS(arr);
        System.out.println("最长连续序列的长度为: " + maxLength);
    }

    /**
     * 代码中的类名、方法名、参数名已经指定，请勿修改，直接返回方法规定的值即可
     * <p>
     * max increasing subsequence
     *
     * @param arr int整型一维数组 the array
     * @return int整型
     */
    public static int MLS(int[] arr) {
        // write code here
        HashSet<Integer> set = new HashSet<>();
        int maxLength = 0;

        for (int num : arr) {
            set.add(num);
        }

        for (int num : arr) {
            if (!set.contains(num - 1)) {
                int currentNum = num;
                int currentLength = 1;

                while (set.contains(currentNum + 1)) {
                    currentNum++;
                    currentLength++;
                }

                maxLength = Math.max(maxLength, currentLength);
            }
        }

        return maxLength;
    }
}
